//
//  ZJRZDetailViewController.m
//  Estate
//
//  Created by Dolion.Hou on 16/7/12.
//  Copyright © 2016年 ttsx. All rights reserved.
//
#import "JJTYPZViewController.h"
#import "SafeDetailViewController.h"
#import "PatrolDetailViewController.h"
#import "TYPZViewController.h"
#import "HNTViewController.h"
#import "HTTViewController.h"
#import "GZZViewController.h"
#import "FSSGViewController.h"
#import "JLAddJLViewController.h"
#import "GJDBViewController_Detail.h"
#import "SGFDBDetailViewController.h"
#import "addDiaryHeadView.h"
#import "DiaryModel.h"

#import "H1_ImagesCell.h"
#import "DoubleLabCell.h"
#import "JLRJTitleCell.h"
#import "DefaineCell.h"
#import "B_wuLiaoCell.h"
#import "ScoureCell.h"

#import "ZJRZDetailViewController.h"


@interface ZJRZDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    DBHelper *_helper;
}

@property (strong ,nonatomic)TPKeyboardAvoidingTableView *tableView;

/**数据模型*/
@property (strong ,nonatomic)DiaryModel *model;

@property (strong, nonatomic) Tweet *curTweet;

//数据源
@property(strong ,nonatomic)NSMutableArray *dataArray;

@property (strong, nonatomic)NSMutableArray *selectedAssetURLs;

@property(strong ,nonatomic) FMDatabase *db;
@property(copy, nonatomic)NSString *scoure;
@property(copy, nonatomic)NSString *pingjia;

@end


@implementation ZJRZDetailViewController

//懒加载
- (NSMutableArray *)dataArray{
    if(_dataArray==nil){
        _dataArray=[NSMutableArray array];
    }
    return  _dataArray;
}

-(FMDatabase *)db{
    if(_db==nil){
        _db=[FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    }
    return _db;
}

- (NSMutableArray *)selectedAssetURLs{
    if(!_selectedAssetURLs){
        _selectedAssetURLs=[NSMutableArray array];
    }
    return _selectedAssetURLs;
}

- (TPKeyboardAvoidingTableView *)tableView{
    if(_tableView==nil){
        _tableView=[[TPKeyboardAvoidingTableView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT)];
        _tableView.delegate=self;
        _tableView.dataSource=self;
        _tableView.bounces=NO;
        [_tableView registerNib:[UINib nibWithNibName:@"DoubleLabCell" bundle:nil] forCellReuseIdentifier:@"dCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"JLRJTitleCell" bundle:nil] forCellReuseIdentifier:@"titleCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"DefaineCell" bundle:nil] forCellReuseIdentifier:@"textCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"B_wuLiaoCell" bundle:nil] forCellReuseIdentifier:@"sCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"ScoureCell" bundle:nil] forCellReuseIdentifier:@"scoureCell"];
        [_tableView registerClass:[H1_ImagesCell class] forCellReuseIdentifier:kCellIdentifier_H1_ImagesCell];
        
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"日记";
    self.view.backgroundColor=[UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1.00];
    //添加右上方提交按钮
    if([self.state isEqualToString:@"0"]){
        self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"wancheng"] style:UIBarButtonItemStyleDone target:self action:@selector(clickSave)];
    }
    [self.view addSubview:self.tableView];
    
    _db = [FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    _helper = [DBHelper help];
    _curTweet = [[Tweet alloc] init];
    [self initData];
    [self refresh:nil];
}

//!!!!:保存视图
- (void)clickSave {
    if([self.scoure isEqualToString:@""]){
        self.scoure=@"60";
    }
    if([self.pingjia isEqualToString:@""]){
        [LSUtility showToastWithMessage:@"请输入评价" inView:self.view Andtime:1.];
        return;
    }
    NSDictionary *dic=@{@"REVIEW_INFO":self.pingjia,
                        @"REVIEW_SCORE":self.scoure,
                        @"REVIEW_STATE":@"1",
                        @"REVIEW_TIME":[LSUtility nowTime],
                        @"MODIFY_TIME":[LSUtility TimeNow],
                        @"LOCAL_MODIFY_STATE":@"MOD"};
    //修改数据表
    [_helper DataBase:_db updateTable:WORK_DIARY setKeyValues:dic whereCondition:@{@"ID":_diaryId}];
    NSNotification * notice = [NSNotification notificationWithName:@"Refresh_zj_rj" object:nil userInfo:nil];
    [[NSNotificationCenter defaultCenter]postNotification:notice];
    // 通知首页刷新数据
    [[NSNotificationCenter defaultCenter] postNotificationName:@"home" object:nil];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)refresh:(id)sender{
    if(self.selectedAssetURLs!=nil){
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            //设置图片
            self.curTweet.selectedAssetURLs =self.selectedAssetURLs;
            //回到主线程来刷新
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:self.dataArray.count+3]] withRowAnimation:UITableViewRowAnimationFade];
            });
        });
    }
}

- (void)initData {
    [self.db open];
    NSString *sqlStr=[NSString stringWithFormat:@" select substr(DIARY_TIME,1,10) AS Day,* from work_diary where id='%@'",self.diaryId];
    FMResultSet *results=[self.db executeQuery:sqlStr];
    _model = [[DiaryModel alloc]init];
    while ([results next]) {
        _model.PRO_ID=[results stringForColumn:@"PRO_ID"];
        _model.DIARY_USER= [LSUtility getuserIdByUserName:
                            [results stringForColumn:@"DIARY_USER"]][@"REAL_NAME"];
        _model.DIARY_TYPE=[results stringForColumn:@"DIARY_TYPE"];
        _model.DIARY_DESC=[results stringForColumn:@"DIARY_DESC"];
        _model.day=[results stringForColumn:@"Day"];
        _model.CREATE_TIME=[results stringForColumn:@"CREATE_TIME"];
        _model.DIARY_TIME=[results stringForColumn:@"DIARY_TIME"];
        _model.TOUR_INFO=[results stringForColumn:@"TOUR_INFO"];
        _model.EXTEND_INFO=[results stringForColumn:@"EXTEND_INFO"];
        _model.CHECK_INFO=[results stringForColumn:@"CHECK_INFO"];
        _model.WEATHER=[results stringForColumn:@"WEATHER"];
        _model.SITE_INFO=[results stringForColumn:@"SITE_INFO"];
        _model.SUPERVISE_INFO=[results stringForColumn:@"SUPERVISE_INFO"];
        _model.FOLLOW_INFO=[results stringForColumn:@"FOLLOW_INFO"];
        _model.SAFE_INFO=[results stringForColumn:@"SAFE_INFO"];
        _model.JIANCHA_INFO=[results stringForColumn:@"JIANCHA_INFO"];
        _model.ZHILIANG_INFO=[results stringForColumn:@"ZHILIANG_INFO"];
        _model.JINDU_INFO=[results stringForColumn:@"JINDU_INFO"];
        _model.CAILIAO_INFO=[results stringForColumn:@"CAILIAO_INFO"];
        _model.YINBI_INFO=[results stringForColumn:@"YINBI_INFO"];
        _model.SHOUFAWEN_INFO=[results stringForColumn:@"SHOUFAWEN_INFO"];
        _model.JIANZHENG_INFO=[results stringForColumn:@"JIANZHENG_INFO"];
        _model.HUIYI_INFO=[results stringForColumn:@"HUIYI_INFO"];
        _model.OTHER_INFO=[results stringForColumn:@"OTHER_INFO"];
        _model.ENTER_EXTER_COMM_INFO=[results stringForColumn:@"ENTER_EXTER_COMM_INFO"];
        _model.MEETING_INFO=[results stringForColumn:@"MEETING_INFO"];
        _model.REVIEW_INFO=[results stringForColumn:@"REVIEW_INFO"];
        _model.PATROL_RECORD_INFO=[results stringForColumn:@"PATROL_RECORD_INFO"];
        _model.SAFE_RECORD_INFO=[results stringForColumn:@"SAFE_RECORD_INFO"];
        
        _model.REVIEW_SCORE=[results stringForColumn:@"REVIEW_SCORE"];
        _model.REVIEW_USER=[results stringForColumn:@"REVIEW_USER"];
        _model.REVIEW_STATE=[results stringForColumn:@"REVIEW_STATE"];
        _model.FILEIDS=[results stringForColumn:@"FILEIDS"];
        _model.STATE=[results stringForColumn:@"STATE"];
        _model.CREATE_USER_NAME=[results stringForColumn:@"CREATE_USER_NAME"];
        _model.CREATE_IP=[results stringForColumn:@"CREATE_IP"];
        _model.LOCAL_MODIFY_STATE=[results stringForColumn:@"LOCAL_MODIFY_STATE"];
        
        //设置附件
        NSArray *imageArr=[LSUtility dictionaryWithJsonString:_model.FILEIDS];
        //查询附件表
        if(imageArr.count>0){
            for (NSDictionary *imageDic in imageArr) {
                NSString *fileId=imageDic[@"fileId"];
                NSString *fileName=[LSUtility queryAttachment:fileId][@"FILE_NAME"];
                NSString *filePath=[DEF_FILE_PATH stringByAppendingString:
                                    [NSString stringWithFormat:@"/%@",fileName]];
                [self.selectedAssetURLs addObject:filePath];
            }
        }
        self.curTweet.selectedAssetURLs =self.selectedAssetURLs;
    }
    self.scoure=_model.REVIEW_SCORE==nil?@"":_model.REVIEW_SCORE;
    self.pingjia=_model.REVIEW_INFO==nil?@"":_model.REVIEW_INFO;
    [_db close];
    [self setData];
}

#pragma  mark- 设置数据
- (void)setData {
    //巡视记录
    NSArray *arr1 =[LSUtility dictionaryWithJsonString:_model.TOUR_INFO];
    //至少有一条数据添加到数据源
    if(arr1.count>0){
        NSDictionary *dic1=@{@"title":@"巡视记录",@"data":arr1};
        [self.dataArray addObject:dic1];
    }
    //验收
    NSArray *arr2 =[LSUtility dictionaryWithJsonString:_model.CHECK_INFO];
    if(arr2.count>0){
        NSDictionary *dic2=@{@"title":@"验收",@"data":arr2};
        [self.dataArray addObject:dic2];
    }
    //旁站
    NSArray *arr3 =[LSUtility dictionaryWithJsonString:_model.SITE_INFO];
    if(arr3.count>0){
        NSDictionary *dic3=@{@"title":@"旁站",@"data":arr3};
        [self.dataArray addObject:dic3];
    }
    //督办
    NSArray *arr4 =[LSUtility dictionaryWithJsonString:_model.SUPERVISE_INFO];
    if(arr4.count>0){
        NSDictionary *dic4=@{@"title":@"督办",@"data":arr4};
        [self.dataArray addObject:dic4];
    }
    //安全记录
    NSArray *arr5 =[LSUtility dictionaryWithJsonString:_model.SAFE_INFO];
    if(arr5.count>0){
        NSDictionary *dic5=@{@"title":@"安全记录",@"data":arr5};
        [self.dataArray addObject:dic5];
    }
    //跟进
    NSArray *arr6 =[LSUtility dictionaryWithJsonString:_model.FOLLOW_INFO];
    if(arr6.count>0){
        NSDictionary *dic6=@{@"title":@"跟进",@"data":arr6};
        [self.dataArray addObject:dic6];
    }
    //其它
    NSArray *arr7 =[LSUtility dictionaryWithJsonString:_model.OTHER_INFO];
    if(arr7.count>0){
        NSDictionary *dic7=@{@"title":@"其它",@"data":arr7};
        [self.dataArray addObject:dic7];
    }
    //检查
    NSArray *arr8 =[LSUtility dictionaryWithJsonString:_model.JIANCHA_INFO];
    if(arr8.count>0){
        NSDictionary *dic=@{@"title":@"检查",@"data":arr8};
        [self.dataArray addObject:dic];
    }
    //质量
    NSArray *arr9 =[LSUtility dictionaryWithJsonString:_model.ZHILIANG_INFO];
    if(arr9.count>0){
        NSDictionary *dic=@{@"title":@"质量",@"data":arr9};
        [self.dataArray addObject:dic];
    }
    //进度
    NSArray *arr10 =[LSUtility dictionaryWithJsonString:_model.JINDU_INFO];
    if(arr10.count>0){
        NSDictionary *dic=@{@"title":@"进度",@"data":arr10};
        [self.dataArray addObject:dic];
    }
    //材料设备进场
    NSArray *arr11 =[LSUtility dictionaryWithJsonString:_model.CAILIAO_INFO];
    if(arr11.count>0){
        NSDictionary *dic=@{@"title":@"材料设备进场",@"data":arr11};
        [self.dataArray addObject:dic];
    }
    //隐蔽检查验收
    NSArray *arr12 =[LSUtility dictionaryWithJsonString:_model.YINBI_INFO];
    if(arr12.count>0){
        NSDictionary *dic=@{@"title":@"隐蔽检查验收",@"data":arr12};
        [self.dataArray addObject:dic];
    }
    //收发文
    NSArray *arr13 =[LSUtility dictionaryWithJsonString:_model.SHOUFAWEN_INFO];
    if(arr13.count>0){
        NSDictionary *dic=@{@"title":@"收发文",@"data":arr13};
        [self.dataArray addObject:dic];
    }
    //见证取样
    NSArray *arr14 =[LSUtility dictionaryWithJsonString:_model.JIANZHENG_INFO];
    if(arr14.count>0){
        NSDictionary *dic=@{@"title":@"见证取样",@"data":arr14};
        [self.dataArray addObject:dic];
    }
    //会议及协调
    NSArray *arr15 =[LSUtility dictionaryWithJsonString:_model.HUIYI_INFO];
    if(arr15.count>0){
        NSDictionary *dic=@{@"title":@"会议及协调",@"data":arr15};
        [self.dataArray addObject:dic];
    }
    // 巡视
    NSArray *arr16 = [LSUtility dictionaryWithJsonString:_model.PATROL_RECORD_INFO];
    if (arr16.count > 0) {
        NSDictionary *dic=@{@"title":@"巡视",@"data":arr16};
        [self.dataArray addObject:dic];
    }
    // 安全
    NSArray *arr17 = [LSUtility dictionaryWithJsonString:_model.SAFE_RECORD_INFO];
    if (arr17.count > 0) {
        NSDictionary *dic=@{@"title":@"安全",@"data":arr17};
        [self.dataArray addObject:dic];
    }
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if([self.identity isEqualToString:@""]){
        return self.dataArray.count+4;
        
    }else{
        return self.dataArray.count+2;
    }
}

// 返回组
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if([self.identity isEqualToString:@""]){
        //得到类别下content
        if(section==0)//第一组
        {
            return 6;
        }else if(section==self.dataArray.count+1)//说明组
        {
            return 1;
        }else if (section==self.dataArray.count+2)//审阅组
        {
            return 3;
        }else if (section==self.dataArray.count+3){//附件组
            return 1;
        }
        else//查询动态添加组
        {
            NSDictionary *dict=self.dataArray[section-1];
            NSMutableArray *arr=[dict objectForKey:@"data"];
            return arr.count;
        }
    }
    else
    {
        //得到类别下content
        if(section==0)//第一组
        {
            return 6;
        }else if(section==self.dataArray.count+1)//倒数第二组
        {
            return 1;
        }
        else//查询动态添加组
        {
            NSDictionary *dict=self.dataArray[section-1];
            NSMutableArray *arr=[dict objectForKey:@"data"];
            return arr.count;
        }
    }
}

//每行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if([self.identity isEqualToString:@""]){
        if(indexPath.section==self.dataArray.count+1)  //说明组
        {
            return 120;
            
        }else if (indexPath.section==self.dataArray.count+2) //审阅组
        {
            if(indexPath.row==2){
                return 120;
            }else{
                return 44;
            }
        }else if (indexPath.section==self.dataArray.count+3){//附件组
            
            return  [H1_ImagesCell cellHeightWithObj:_curTweet title:@"附件信息"];
        }
        else
        {
            return 44;
        }
    }
    else
    {
        if(indexPath.section==self.dataArray.count+1)  //倒数第二组
        {
            return 120;
        }
        else
        {
            return 44;
        }
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //第一组
    if (indexPath.section==0) {
        if (indexPath.row==0) {
            DoubleLabCell  *cell=[tableView dequeueReusableCellWithIdentifier:@"dCell" forIndexPath:indexPath];
            cell.backgroundColor=[UIColor colorWithRed:0.85 green:0.93 blue:0.97 alpha:1.00];
            //标题
            NSString *title=[NSString stringWithFormat:@">>%@ %@ 监理日记",_model.day,!_model.DIARY_USER?@"已移除":_model.DIARY_USER];
            //审阅否
            NSString *state;
            if([_model.REVIEW_STATE isEqualToString:@"0"]){
                state=@"状态:未审阅";
            }else{
                state=@"状态:已审阅";
            }
            [cell setTitle:title AndTitleColor:[UIColor colorWithRed:0.24 green:0.64 blue:0.61 alpha:1.00] AndDetail:state AndDetailColor:[UIColor colorWithRed:0.88 green:0.35 blue:0.42 alpha:1.00]];
            return cell;
            
        } else {
            if(indexPath.row==1){
                DefaineCell *cell=[tableView dequeueReusableCellWithIdentifier:@"textCell" forIndexPath:indexPath];
                //根据id 查询项目名
                NSString *proname=[LSUtility  getProjectNameByProjectId:_model.PRO_ID][@"PROJECT_NAME"];
                [cell setTitle:@"项    目:" andDetail:proname andLeftConstraint:8];
                return cell;
                
            }else if (indexPath.row==2){
                DefaineCell *cell=[tableView dequeueReusableCellWithIdentifier:@"textCell" forIndexPath:indexPath];
                [cell setTitle:@"记 录 人:" andDetail:!_model.DIARY_USER?@"已移除":_model.DIARY_USER andLeftConstraint:8];
                return cell;
                
            }if (indexPath.row==3){
                DefaineCell *cell=[tableView dequeueReusableCellWithIdentifier:@"textCell" forIndexPath:indexPath];
                [cell setTitle:@"时    间:" andDetail:_model.DIARY_TIME andLeftConstraint:8];
                return cell;
                
            }else if(indexPath.row==4){
                DefaineCell *cell=[tableView dequeueReusableCellWithIdentifier:@"textCell" forIndexPath:indexPath];
                NSString *diqu=[[_model.WEATHER componentsSeparatedByString:@"/"]firstObject];
                [cell setTitle:@"地    区:" andDetail:diqu andLeftConstraint:8];
                return cell;
                
            }else {
                DefaineCell *cell=[tableView dequeueReusableCellWithIdentifier:@"textCell" forIndexPath:indexPath];
                NSString *weather=[[_model.WEATHER componentsSeparatedByString:@"/"]lastObject];
                [cell setTitle:@"天   气:" andDetail:weather andLeftConstraint:8];
                return cell;
            }
        }
    }else if (indexPath.section==self.dataArray.count+1)//倒数第二组
    {
        B_wuLiaoCell *cell=[tableView dequeueReusableCellWithIdentifier:@"sCell" forIndexPath:indexPath];
        
        cell.textValueChangedBlock=^(NSString *value){
        };
        [cell setTitle:@"说  明" andPlaceHolder:@"请手动填写" andTextValue: _model.DIARY_DESC];
        cell.isEdit=NO;
        return cell;
        
    }else if(indexPath.section==self.dataArray.count+2)//最后一组
    {
        if(indexPath.row==0)
        {
            DefaineCell *cell=[tableView dequeueReusableCellWithIdentifier:@"textCell" forIndexPath:indexPath];
            NSString *name=[LSUtility getuserIdByUserName:_model.REVIEW_USER][@"REAL_NAME"];
            [cell setTitle:@"审阅人:" andDetail:name andLeftConstraint:8];
            
            return cell;
        }
        else if (indexPath.row==1)
        {
            ScoureCell *cell=[tableView dequeueReusableCellWithIdentifier:@"scoureCell" forIndexPath:indexPath];
            cell.scoreLimit=@"100";
            cell.view = self.navigationController.view;
            if([self.state isEqualToString:@"1"]){
                cell.isEdit=NO;
            }
            cell.textFile.text=[self.scoure isEqualToString:@""]?@"60":self.scoure;
            cell.textBlock=^(NSString *value){
                self.scoure=value;
                NSLog(@"分数:%@",self.scoure);
            };
            return cell;
            
        }else{
            B_wuLiaoCell *cell=[tableView dequeueReusableCellWithIdentifier:@"sCell" forIndexPath:indexPath];
            if([self.state isEqualToString:@"1"]){
                cell.isEdit=NO;
            }
            [cell setTitle:@"评  价" andPlaceHolder:@""  andTextValue:self.pingjia];

            cell.textValueChangedBlock=^(NSString *value){
                self.pingjia=value;
            };
            return cell;
        }
        
    }else if(indexPath.section==self.dataArray.count+3){
        H1_ImagesCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_H1_ImagesCell forIndexPath:indexPath];
        cell.isEdit=NO;
        [cell setTitle:@"附件信息"];
        [cell setTitleColor:[UIColor colorWithRed:0.18 green:0.64 blue:0.60 alpha:1.00]];
        cell.curTweet = _curTweet;
        return cell;
        
    }else{
        JLRJTitleCell *cell=[tableView dequeueReusableCellWithIdentifier:@"titleCell" forIndexPath:indexPath];
        //判断是否是查看页面
        if([self.flag isEqualToString:@"look"]){
            cell.str=@"look";
        }
        cell.isEdit=NO;
        NSDictionary *dict=self.dataArray[indexPath.section-1];
        NSMutableArray *arr=[dict objectForKey:@"data"];
        NSDictionary *cellDataDic=arr[indexPath.row];
        cell.title.text=cellDataDic[@"info"];
        //执行编辑操作
        cell.textValueChangedBlock=^(NSString *value){
            //重新设置标题
            [cellDataDic setValue:value forKey:@"info"];
        };
        
        //执行删除操作
        cell.dltBlock=^{
            [arr removeObjectAtIndex:indexPath.row];
            //该组下没有任何内容 移除改组
            if(arr.count==0){
                NSLog(@"清空该组");
            }
            [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationFade)];
            [self.tableView reloadData];
        };
        WEAKSELF
        //详情信息
        cell.detailBlick=^{
            // 跳转控制器
            [weakSelf jumpCtrforType:cellDataDic[@"recordType"] andId:cellDataDic[@"recordId"]];
        };
        return cell;
    }
}

// 组尾高
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if([self.identity isEqualToString:@""]){
        if(section==0||section==self.dataArray.count+1){
            return 15;
        }else
            return 1;
    }
    else
    {
        if(section==0||section==self.dataArray.count+1){
            return 15;
        }else
            return 1;
    }
}

//组尾
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    //第一组 和倒数第二组
    if(section==0||section==self.dataArray.count+1)
    {
        UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH-20, 15)];
        view.backgroundColor=[UIColor colorWithRed:0.95 green:0.96 blue:0.96 alpha:1.00];
        //波浪线
        UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, 8)];
        img.backgroundColor=[UIColor colorWithRed:0.95 green:0.96 blue:0.96 alpha:1.00];
        img.image=[UIImage imageNamed:@"juchishang"];
        [view addSubview:img];
        return view;
        
    }else{
        //横线
        UIView *line=[[UIView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, 1)];
        line.backgroundColor=[UIColor colorWithRed:0.89 green:0.89 blue:0.90 alpha:1.00];
        return line;
    }
}

//组头高
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if([self.identity isEqualToString:@""]){
        if(section==0||section==self.dataArray.count+1||section==self.dataArray.count+2){
            return 0.1;
        }else if (section==1){
            return 30.;
        }else
            return 25;
    }
    else{
        if(section==0||section==self.dataArray.count+1){
            return 0.1;
        }else if (section==1){
            return 30.;
        }else
            return 25;
    }
}

//组头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if([self.identity isEqualToString:@""]){
        //第一组 最后一组
        if(section==0 ||section==self.dataArray.count+1||section==self.dataArray.count+2||section==self.dataArray.count+3)
        {
            return nil;
            
        }else if(section==1)
        {
            addDiaryHeadView *headView=[[addDiaryHeadView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, 30)];
            headView.title.text=self.dataArray[section-1][@"title"];
            return headView;
        }
        else {
            UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, 25)];
            UILabel *titleLab=[[UILabel alloc]initWithFrame:CGRectMake(8, 8, DEF_SCREEN_WIDTH, 15)];
            titleLab.font=[UIFont systemFontOfSize:15.];
            view.backgroundColor=[UIColor whiteColor];
            titleLab.textColor=[UIColor colorWithRed:0.47 green:0.48 blue:0.49 alpha:1.00];
            titleLab.text=self.dataArray[section-1][@"title"];
            [view addSubview:titleLab];
            return view;
        }
    }
    else
    {
        //第一组 最后一组
        if(section==0 ||section==self.dataArray.count+1)
        {
            return nil;
            
        }else if(section==1)
            
        {
            addDiaryHeadView *headView=[[addDiaryHeadView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, 30)];
            headView.title.text=self.dataArray[section-1][@"title"];
            return headView;
        }
        else {
            UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, 25)];
            UILabel *titleLab=[[UILabel alloc]initWithFrame:CGRectMake(8, 8, DEF_SCREEN_WIDTH, 15)];
            titleLab.font=[UIFont systemFontOfSize:15.];
            view.backgroundColor=[UIColor whiteColor];
            titleLab.textColor=[UIColor colorWithRed:0.47 green:0.48 blue:0.49 alpha:1.00];
            titleLab.text=self.dataArray[section-1][@"title"];
            [view addSubview:titleLab];
            return view;
        }
    }
}

#pragma mark- 根据类型 。跳转到相应的页面查看详情
- (void)jumpCtrforType:(NSString *)type andId:(NSString *)id{
    
    JLAddJLViewController *jilCtr=[[JLAddJLViewController alloc]init];
    //根据id 查询最后修改时间
    NSDictionary *dic=[self modifyForId:id];
    NSString *extend=dic[@"ADDRES_INFO"];
    NSString *beginTime=dic[@"BEGIN_TIME"];
    NSString *endTime=dic[@"END_TIME"];
    
    if([type isEqualToString:@"RECORD_01"]||[type isEqualToString:@"RECORD_03"]||[type isEqualToString:@"RECORD_04"]||[type isEqualToString:@"RECORD_07"]||[type isEqualToString:@"RECORD_08"]||[type isEqualToString:@"RECORD_09"]||[type isEqualToString:@"RECORD_10"]||[type isEqualToString:@"RECORD_11"]||[type isEqualToString:@"RECORD_12"]||[type isEqualToString:@"RECORD_13"]||[type isEqualToString:@"RECORD_14"])//巡视 安全 其他
    {
        jilCtr.recordId=id;
        jilCtr.isEdit=NO;
        [self.navigationController pushViewController:jilCtr animated:YES];
    }
    else if ([type isEqualToString:@"RECORD_02_01"])//旁站通用
    {
        TYPZViewController *typzCtr=[[TYPZViewController alloc]init];
        typzCtr.isSubStr=@"flag";
        typzCtr.sideId=id;
        [self.navigationController pushViewController:typzCtr animated:YES];
        
    }
    else if ([type isEqualToString:@"RECORD_02_02"])//旁站防水
    {
        FSSGViewController *fssgCtr=[[FSSGViewController alloc]init];
        fssgCtr.isSubStr=@"flag";
        fssgCtr.sideId=id;
        [self.navigationController pushViewController:fssgCtr animated:YES];
    }
    else if ([type isEqualToString:@"RECORD_02_03"])//旁站回填土
    {
        HTTViewController *httCtr=[[HTTViewController alloc]init];
        httCtr.isSubStr=@"flag";
        httCtr.sideId=id;
        [self.navigationController pushViewController:httCtr animated:YES];
    }
    else if ([type isEqualToString:@"RECORD_02_04"])//旁站混泥土
    {
        HNTViewController *hntCtr=[[HNTViewController alloc]init];
        hntCtr.isSubStr=@"flag";
        hntCtr.sideId=id;
        [self.navigationController pushViewController:hntCtr animated:YES];
    }
    else if ([type isEqualToString:@"RECORD_02_05"])//旁站混泥土灌注桩
    {
        GZZViewController *gzzCtr=[[GZZViewController alloc]init];
        
        gzzCtr.isSubStr=@"flag";
        gzzCtr.sideId=id;
        
        [self.navigationController pushViewController:gzzCtr animated:YES];
    }else if([type isEqualToString:@"RECORD_02_JJ_01"]||[type isEqualToString:@"RECORD_02_JJ_02"]||[type isEqualToString:@"RECORD_02_JJ_03"]||[type isEqualToString:@"RECORD_02_JJ_04"]||[type isEqualToString:@"RECORD_02_JJ_05"]||[type isEqualToString:@"RECORD_02_JJ_06"]){
        
        JJTYPZViewController *jjtyCtr = [[JJTYPZViewController alloc]init];
        NSString *titleStr = @"";
        if ([type isEqualToString:@"RECORD_02_JJ_01"]) {
            titleStr = @"通用";
            
        } else if ([type isEqualToString:@"RECORD_02_JJ_02"]) {
            titleStr = @"桩基钢筋笼安放";
            
        } else if ([type isEqualToString:@"RECORD_02_JJ_03"]) {
            titleStr = @"灌注桩首盘砼浇筑";
            
        } else if ([type isEqualToString:@"RECORD_02_JJ_04"]) {
            titleStr = @"路面(沥青)混凝土摊铺";
            
        } else if ([type isEqualToString:@"RECORD_02_JJ_05"]) {
            titleStr = @"预应力张拉";
            
        } else if ([type isEqualToString:@"RECORD_02_JJ_06"]) {
            titleStr = @"预应力压浆";
        }
        jjtyCtr.title = titleStr;
        jjtyCtr.isSubStr= type;
        jjtyCtr.sideId = id;
        [self.navigationController pushViewController:jjtyCtr animated:YES];
    }
    
    else if ([type isEqualToString:@"1"])//督办
    {
        GJDBViewController_Detail *detail=[[GJDBViewController_Detail alloc]init];
        detail.beginTime=beginTime;
        detail.endTime=endTime;
        detail.addressInfo=extend;
        detail.processId=id;
        [self.navigationController pushViewController:detail animated:YES];
    }
    else if ([type isEqualToString:@"2"])//跟进
    {
        SGFDBDetailViewController *GJDetail=[[SGFDBDetailViewController alloc]init];
        GJDetail.beginTime=beginTime;
        GJDetail.endTime=endTime;
        GJDetail.flag = @"detail";
        GJDetail.addressInfo=extend;
        GJDetail.processID =id;
        GJDetail.gengjing = YES;
        [self.navigationController pushViewController:GJDetail animated:YES];
    }
    else if ([type isEqualToString:@"PATROL_01"])
    { // 巡视
        PatrolDetailViewController *patrolVC = [[PatrolDetailViewController alloc] init];
        patrolVC.isDiary = YES;
        patrolVC.patrolModel = [self selectPatrolInformation:id];
        [self.navigationController pushViewController:patrolVC animated:YES];
    }
    else if ([type isEqualToString:@"SAFE_RECORD_01"] || [type isEqualToString:@"SAFE_RECORD_02"] || [type isEqualToString:@"SAFE_RECORD_03"] || [type isEqualToString:@"SAFE_RECORD_04"] || [type isEqualToString:@"SAFE_RECORD_05"])    // 安全
    {
        SafeDetailViewController *safeVC = [[SafeDetailViewController alloc] init];
        safeVC.isDiary = YES;
        safeVC.safeModel = [self selectSafeInformation:id];
        [self.navigationController pushViewController:safeVC animated:YES];
    }
}

// 查找巡视信息
- (PatrolRecord *)selectPatrolInformation:(NSString *)safeID {
    FMDatabase *db = [FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    [db open];
    // 查找基本数据
    NSString *baseStr = [NSString stringWithFormat:@"SELECT * FROM PATROL_RECORD WHERE ID = '%@'", safeID];
    
    PatrolRecord *model = [[PatrolRecord alloc]init];
    FMResultSet *results = [db executeQuery:baseStr];
    while ([results next]) {
        model.id = [results stringForColumn:@"ID"];
        model.proId = [results stringForColumn:@"PRO_ID"];
        model.supervisorProUnitId = [results stringForColumn:@"SUPERVISOR_PRO_UNIT_ID"];
        model.constructProUnitId = [results stringForColumn:@"CONSTRUCT_PRO_UNIT_ID"];
        model.supervisorProUnitName = [results stringForColumn:@"SUPERVISOR_PRO_UNIT_NAME"];
        model.constructProUnitName = [results stringForColumn:@"CONSTRUCT_PRO_UNIT_NAME"];
        model.supervisorContractNumber = [results stringForColumn:@"SUPERVISOR_CONTRACT_NUMBER"];
        model.constructContractNumber = [results stringForColumn:@"CONSTRUCT_CONTRACT_NUMBER"];
        model.recordUser = [results stringForColumn:@"RECORD_USER"];
        model.recordType = [results stringForColumn:@"RECORD_TYPE"];
        model.recordTitle = [results stringForColumn:@"RECORD_TITLE"];
        model.recordContent = [results stringForColumn:@"RECORD_CONTENT"];
        model.state = [results stringForColumn:@"STATE"];
        model.recordNumber = [results stringForColumn:@"RECORD_NUMBER"];
        model.startTime = [results stringForColumn:@"START_TIME"];
        model.endTime = [results stringForColumn:@"END_TIME"];
        model.startAndEndTime = [results stringForColumn:@"START_AND_END_TIME"];
        model.weather = [results stringForColumn:@"WEATHER"];
        model.addId = [results stringForColumn:@"ADD_ID"];
        model.addInfo = [results stringForColumn:@"ADD_INFO"];
        model.rangeAndPosition = [results stringForColumn:@"RANGE_AND_POSITION"];
        model.majorproAndPerson = [results stringForColumn:@"MAJORPRO_AND_PERSON"];
        model.majorInspectData = [results stringForColumn:@"MAJOR_INSPECT_DATA"];
        model.problemAndDeal = [results stringForColumn:@"PROBLEM_AND_DEAL"];
        model.extendInfo = [results stringForColumn:@"EXTEND_INFO"];
        model.createusername = [results stringForColumn:@"CREATE_USER_NAME"];
        model.createtime = [results stringForColumn:@"CREATE_TIME"];
        model.createip = [results stringForColumn:@"CREATE_IP"];
        model.modifyusername = [results stringForColumn:@"MODIFY_USER_NAME"];
        model.modifytime = [results stringForColumn:@"MODIFY_TIME"];
        model.modifyip = [results stringForColumn:@"MODIFY_IP"];
    }
    [db close];
    return model;
}

// 查找安全信息
- (SecurityRecordBasic *)selectSafeInformation:(NSString *)safeID {
    FMDatabase *db = [FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    [db open];
    // 查找基本数据
    NSString *baseStr = [NSString stringWithFormat:@"SELECT * FROM SAFE_RECORD_BASIC WHERE ID = '%@'", safeID];
    
    SecurityRecordBasic *model = [[SecurityRecordBasic alloc] init];
    FMResultSet *baseResult = [db executeQuery:baseStr];
    while ([baseResult next]) {
        model.id = [baseResult stringForColumn:@"ID"];
        model.proId = [baseResult stringForColumn:@"PRO_ID"];
        model.supervisorProUnitId = [baseResult stringForColumn:@"SUPERVISOR_PRO_UNIT_ID"];
        model.constructProUnitId = [baseResult stringForColumn:@"CONSTRUCT_PRO_UNIT_ID"];
        model.supervisorProUnitName = [baseResult stringForColumn:@"SUPERVISOR_PRO_UNIT_NAME"];
        model.constructProUnitName = [baseResult stringForColumn:@"CONSTRUCT_PRO_UNIT_NAME"];
        model.supervisorContractNumber = [baseResult stringForColumn:@"SUPERVISOR_CONTRACT_NUMBER"];
        model.constructContractNumber = [baseResult stringForColumn:@"CONSTRUCT_CONTRACT_NUMBER"];
        model.recordUser = [baseResult stringForColumn:@"RECORD_USER"];
        model.recordType = [baseResult stringForColumn:@"RECORD_TYPE"];
        model.recordTitle = [baseResult stringForColumn:@"RECORD_TITLE"];
        model.recordContent = [baseResult stringForColumn:@"RECORD_CONTENT"];
        model.state = [baseResult stringForColumn:@"STATE"];
        model.recordNumber = [baseResult stringForColumn:@"RECORD_NUMBER"];
        model.supervisorOpinion = [baseResult stringForColumn:@"SUPERVISOR_OPINION"];
        model.chiefSupervisorOpinion = [baseResult stringForColumn:@"CHIEF_SUPERVISOR_OPINION"];
        model.projectContent = [baseResult stringForColumn:@"PROJECT_CONTENT"];
        model.otherInfo = [baseResult stringForColumn:@"OTHER_INFO"];
        model.weather = [baseResult stringForColumn:@"WEATHER"];
        model.addId = [baseResult stringForColumn:@"ADD_ID"];
        model.addInfo = [baseResult stringForColumn:@"ADD_INFO"];
        model.extendInfo = [baseResult stringForColumn:@"EXTEND_INFO"];
        model.createusername = [baseResult stringForColumn:@"CREATE_USER_NAME"];
        model.createtime = [baseResult stringForColumn:@"CREATE_TIME"];
        model.createip = [baseResult stringForColumn:@"CREATE_IP"];
        model.modifyusername = [baseResult stringForColumn:@"MODIFY_USER_NAME"];
        model.modifytime = [baseResult stringForColumn:@"MODIFY_TIME"];
        model.modifyip = [baseResult stringForColumn:@"MODIFY_IP"];
        model.constructContractMoney = [baseResult stringForColumn:@"CONSTRUCT_CONTRACT_MONEY"];
        model.recifyState = [baseResult stringForColumn:@"RECIFY_STATE"];
        model.problemAndDeal = [baseResult stringForColumn:@"PROBLEM_AND_DEAL"];
        model.chiefSupervisorOpinionState = [baseResult stringForColumn:@"CHIEF_SUPERVISOR_OPINION_STATE"];
    }
    [db close];
    return model;
}

- (NSDictionary *)modifyForId:(NSString *)ids{
    DBHelper *helper=[DBHelper help];
    NSArray *dicArr= [helper DataBase:self.db selectKeyTypes:@{@"MODIFY_TIME":@"text",@"ADDRES_INFO":@"text",@"EXTEND_INFO":@"text",@"END_TIME":@"text",@"BEGIN_TIME":@"text"} fromTable:PROCESS whereCondition:@{@"ID":ids}] ;
    return  [dicArr firstObject];
}


@end

